package br.com.felix.bmt.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import br.com.felix.fwt.security.exception.UnauthorizedAccessException;
import br.com.felix.fwt.servlet.exception.UpdateFormFieldsException;
import br.com.felix.fwt.ui.BaseComponent;
import br.com.felix.fwt.ui.Form;
import br.com.felix.fwt.ui.css.Color;
import br.com.felix.fwt.ui.css.ExternalCSS;
import br.com.felix.fwt.ui.exception.UserInterfaceInitializationException;

/**
 * Default implementation of Form for the project.
 * Acts as a template form.
 * */
public abstract class BlogManagementForm  extends Form{

	private static final long serialVersionUID = 4340492965941975319L;

	protected Color maleColor = new Color(153,204,255);
	
	protected Color femaleColor = new Color(232,81,222);
	
	
	/**
	 * Default constructor, defines a default external css style.
	 * */
	public BlogManagementForm(){
		ExternalCSS eCss = new ExternalCSS("../include/style.css");
		referenceStyle(eCss);
	}
	
	
	/**
	 * @see Form#catchUnauthorizedAccessException(UnauthorizedAccessException)
	 * */
	@Override
	public Form catchUnauthorizedAccessException(UnauthorizedAccessException e) {
		ErrorPage ePage = new ErrorPage("You are not authorized.",e);
		ePage.initializeUserInterface();
		return ePage;
	}

	
	/**
	 * @see Form#catchUpdateFormFieldsException(UpdateFormFieldsException)
	 * */
	@Override
	public Form catchUpdateFormFieldsException(UpdateFormFieldsException e) {
		ErrorPage ePage = new ErrorPage("An internal server error has ocurred.",e);
		ePage.initializeUserInterface();
		return ePage;
	}

	
	/**
	 * @see Form#catchUserInterfaceInitializationException(UserInterfaceInitializationException)
	 * */
	@Override
	public Form catchUserInterfaceInitializationException(UserInterfaceInitializationException e) {
		ErrorPage ePage = new ErrorPage("Could not load page.",e);
		ePage.initializeUserInterface();
		return ePage;
	}

	
	/**
	 * Sets a default title.
	 * */
	@Override
	public String getTitle() {
		return this.getClass().getSimpleName() + " - BlogManagement";
	}


	/**
	 * Prints a template HTML.
	 * Note that this is a little complicated to do without knowing 
	 * the framework's implementation.
	 * */
	@Override
	public void write(PrintWriter out) throws IOException {
		List<BaseComponent> thechilds = this.childs; //remember the components "tree structure"
		this.childs = new ArrayList<BaseComponent>(); //redefine the component tree.
		BaseComponent table = new BaseComponent() { //create the html template: just add a table around the components. 
			private static final long serialVersionUID = -4186222106462358227L;
			@Override
			public void write(PrintWriter out) throws IOException {
				out.write("<table cellspacing=\"0\" cellpadding=\"0\" style=\"position:absolute; left: " +
						"15%; top:0px; background-color:white;\">\n<tr>\n<td style=\"left:auto;\">" +
						"<h1>Blog Management</h1></td>\n</tr>\n<tr>\n<td style=\"padding:0px\">\n");
				writeChilds(out); //now I print the components ***.
				out.write("\n</td>\n</tr>\n</table>\n");
			}
		};
		this.childs.add(table);
		table.childs = thechilds; //set the components to print ***.
		super.write(out);
		this.childs = thechilds; //now that everything is printed, behave normally.
	}

}
